#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QApplication>
#include <QWindow>
#include <QQuickItem>
#include <QQuickWindow>
#include <QHBoxLayout>
#include "VideoWidget.h"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);
//    QApplication widgetApp(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
        &app, [&engine, url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl) {
                QCoreApplication::exit(-1);
            }

            QList<QObject*> objs = engine.rootObjects();
            if (obj != nullptr) {
                qDebug() << "root obj name: " << obj->objectName();
                QWindow *rootWindow = qobject_cast<QWindow*>(obj);
                if (rootWindow != nullptr) {
                    qDebug() << "winId: "  << (int)rootWindow->winId();
                }
            }

            for (QObject *pObj : objs) {
                auto pTmp = pObj->findChild<QObject*>("video");
                if (pTmp != nullptr) {
                    QQuickItem *pQuickItem = qobject_cast<QQuickItem*>(pTmp);
                    if (pQuickItem != nullptr) {
                        QQuickWindow *pQuickWindow = pQuickItem->window();
                        if (pQuickWindow != nullptr) {
                            qDebug() << "left winId: " << pQuickWindow->winId();
                        }
                    }
                }
            }


        }, Qt::QueuedConnection);

//    qmlRegisterType<VideoWidget>("an.gdu.VideoWidget", 1, 0, "VideoWidget");

    engine.load(url);

    return /*widgetApp.exec() &*/ app.exec();
}
